<template>
  <div class="box-main1">
    <div class="box-header2">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin/index' }"
          >控制台</el-breadcrumb-item
        >
        <el-breadcrumb-item>老师信息</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div style="padding: 10px;">
      <div style="dispaly: inline-block;" class="box-input2">
        <el-button size="mini" type="warning" icon="el-icon-plus" @click="clkBtnAdd()">新增</el-button>
      </div>
      <el-table class="tbl1" :data="teacherList" :stripe="true" size="small">
        <el-table-column type="index" index="noid" label="序号" width="70"></el-table-column>
        <el-table-column label="老师姓名"  prop="teacher_name" width="150"></el-table-column>
        <el-table-column label="绑定账号"  prop="bind_usercode" min-width="200"></el-table-column>
        <el-table-column label="操作"  prop="bind_usercode" width="200">
          <template slot-scope="scope">
            <span>
              <el-button type="primary" size="mini" @click="clkBtnShowAddStudent(scope.row )">班级</el-button>
              <el-button type="primary" size="mini" @click="clkBtnEdit(scope.row )">编辑</el-button>
              <el-button type="warning" size="mini" @click="clkBtnDelete(scope.row )">删除</el-button>
            </span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      hide-on-single-page
      @current-change="handleCurrentChange"
      layout="prev, pager, next, jumper"
      :page-size="pageSize"
      :total="pages"
      class="page">
    </el-pagination>
    </div>
    <el-dialog title="新增教师信息" class="dialog1"
               :show-close="false" :close-on-click-modal="false"
               :visible.sync="showSaveteStudentDialog">
      <div>
        <div class="box-content2">
          <div class="box-table2" style="width:420px">
            <el-form size="mini" ref="student" :model="student" label-width="120px">
              <el-form-item label="老师姓名">
                <el-input v-model="teacherName"></el-input>
              </el-form-item>
              <el-form-item label="绑定登录账号">
                <el-input v-model="user_code">
                  <el-button slot="append" @click="clkExamUsercode">验证</el-button>
                </el-input>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="clkBtnSave">确 定</el-button>
        <el-button size="mini" @click="showSaveteStudentDialog = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑教师信息" class="dialog1"
               :show-close="false" :close-on-click-modal="false"
               :visible.sync="showSaveteStudentDialogBl">
      <div>
        <div class="box-content2">
          <div class="box-table2" style="width:420px">
            <el-form size="mini" ref="student" :model="student" label-width="120px">
              <el-form-item label="老师姓名">
                <el-input v-model="teacherNamexg"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="xgBtnSave">确 定</el-button>
        <el-button size="mini" @click="showSaveteStudentDialogBl = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/common/utils/request";

export default {
  data() {
    return {
      pageNum: 1, //当前页数
      pageSize: 2, //每页数据条数
      teacherList: [], //每页数据
      pages: null, //总页数
      teacherName: '', //老师姓名
      user_code: '', //用户账号
      showSaveteStudentDialog: false, 
      showSaveteStudentDialogBl: false,
      teacherNamexg: '', //修改老师姓名
      teacherId: '',
    };
  },
  watch: {
    $route: {
      handler: function (to, from) {
        if (to.path == "/admin/teacher_list") {
        }
      },
      immediate: true,
    },
  },
  methods: {
    clkBtnEdit(data){
      //编辑
      this.teacherNamexg = data.teacher_name
      this.teacherId = data.noid
      this.showSaveteStudentDialogBl = true
    },
    xgBtnSave() {
      const url1 = this.settings.apiUrl + "/teacher/save";
      const d1 = {
        teacher_name: this.teacherNamexg,
        noid: this.teacherId,
      };
      request
        .post(url1, d1)
        .then((data) => {
          if (data.code === 0) {
            this.$alert('修改成功')
            this.showSaveteStudentDialogBl = false
            this.getTeacherList();
          } else {
            console.log(data);
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getTeacherList();
    },
    clkBtnAdd() {
      this.showSaveteStudentDialog = true
    },
    clkExamUsercode(){
      if(this.user_code === "" || this.teacherName === ""){
        alert('班级和老师不能为空')
        return
      }
      const url1 = this.settings.apiUrl + "/user_info/validUserCode";
      const d1 = {
        usercode: this.user_code,
      };
      request
        .post(url1, d1)
        .then((d1r) => {
          if (d1r.code == 0) {
            this.$message("可以使用~");
          } else {
            this.$confirm('该账号不存在，是否创建?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'success'
            }).then(() => {

              // 确定要创建的时候， 再次使用request 执行快速创建
              let url2 = this.settings.apiUrl + "/user_info/add";
              let d2 = {'usercode': this.user_code, 'userpwd' : '111111',
                'realname': this.teacherName,
                'permissions':[]};

              request.post(url2, d2).then(d2r => {
                if (d2r.code == 0) {
                  this.$message("创建成功~");

                }


              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消创建'
              });
            });
          }
        })
      
    },
    clkBtnSave(){
      const url1 = this.settings.apiUrl + "/teacher/save";
      const d1 = {
        teacher_name: this.teacherName,
        bind_usercode: this.user_code,
      };
      request
        .post(url1, d1)
        .then((data) => {
          if (data.code === 0) {
            this.$alert('添加成功')
            this.getTeacherList();
          } else {
            console.log(data);
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    clkBtnDelete(data){
      const url1 = this.settings.apiUrl + "/teacher/delete";
      const d1 = {
        noid: data.noid,
      };
      request
        .post(url1, d1)
        .then((data) => {
          if (data.code === 0) {
            alert('删除成功')
            this.getTeacherList();
          } else {
            console.log(data);
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    getTeacherList() {
      const url1 = this.settings.apiUrl + "/teacher/list";
      const d1 = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      request
        .post(url1, d1)
        .then((data) => {
          if (data.code === 0) {
            this.teacherList = data.data.list;
            this.pages = data.data.pages
            console.log(data.data);
          } else {
            console.log(data);
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
  },
  mounted() {
    this.getTeacherList();
  },
};
</script>

<style lang="scss" scoped="scoped">
.box-main2 {
  height: 100%;
  background-repeat: no-repeat;
  background-size: cover;
}

.box-header2 {
  height: 30px;
  background-color: #eceeef;
  padding-left: 20px;
  padding-top: 8px;
}

.page{
  margin: 20px 0;
  float: right;
}
</style>
